var dataDetail = null;

;layui.define(function(e) {
    layui.use(['table','layer','form','laydate','util'], function(){
        var $ = layui.$
            ,layer = layui.layer
            ,form = layui.form
            ,table = layui.table
            ,laydate = layui.laydate;
        //展示已知数据
        table.render({
            elem: '#idTable'
            ,id: 'listReload'
            ,url: hostUrl+'/clue/storeSaleAccCluePoolList?dealerId='+globalDealerId+"&storeId="+globalStoreId
            ,cellMinWidth: 180
            ,loading: true
            ,cols: [[ //标题栏
//                {type:'checkbox', fixed: 'left'},
                {field: 'name', title: '客户姓名', minWidth: 80}
                ,{field: 'sex', title: '性别', minWidth: 80,templet: function(d){
                	if(d.sex == '0'){
	                    return '男';
                	}else if(d.sex == '1'){
	                    return '女';
                	}else{
	                    return '-';
                	}
                }}
                ,{field: 'phone', title: '手机号', minWidth: 130}
                ,{field: 'type', title: '客户类型',templet: function(d){
                	if(d.type == '1'){
	                    return '个人客户';
                	}else if(d.type == '2'){
	                    return '企业客户';
                	}else{
	                    return '-';
                	}
                }}
                ,{field: 'car_info', title: '意向车型', minWidth: 180}
                ,{field: 'store_name', title: '门店'}
                ,{field: 'create_time', title: '跟进日期', minWidth: 180,templet: function(d){
                	return layui.util.toDateString(d.create_time, 'yyyy-MM-dd HH:mm:ss');
                }}
                ,{field: 'clue_source_type', title: '信息来源', minWidth: 150,templet: function(d){
                	if(d.clue_source_type == '0'){
	                    return '线上App';
                	}else if(d.clue_source_type == '1'){
	                    return '线上H5分享';
                	}else if(d.clue_source_type == '2'){
	                    return '平台建档';
                	}else if(d.clue_source_type == '3'){
	                    return '门店建档';
                	}else if(d.clue_source_type == '4'){
	                    return '销售顾问建档';
                	}else if(d.clue_source_type == '5'){
	                    return '经纪人推荐';
                	}else if(d.clue_source_type == '6'){
	                    return '直营店开业（抽奖活动H5）';
                	}else{
	                    return '-';
                	}
                }}
//                ,{field: 'brokerName', title: '经纪人'}
                ,{field: 'clue_o_level', title: '客户等级', minWidth: 50,templet: function(d){
                	if(d.clue_o_level == null || d.clue_o_level == 'null' || d.clue_o_level == ''){
                		if(d.clue_level == null || d.clue_level == 'null' || d.clue_level == ''){
                			return "-";
                		}else{
                			return d.clue_level;
                		}
                	}else{
                		if(d.clue_o_level == null || d.clue_o_level == 'null' || d.clue_o_level == ''){
                			return "-";
                		}else{
                			return d.clue_o_level;
                		}
                	}
                }}
                ,{field: 'remaFollowUTime', title: '距离下次跟进时间', minWidth:160,templet: function(d){
                	if(d.remaFollowUTime.length >= 2){
                		var s = d.remaFollowUTime.substring(0,2);
                		if(s == '不足' || s == '逾期'){
                			return "<span style='color:red'>"+d.remaFollowUTime+"</span>"; 
                		}else{
                			return d.remaFollowUTime;
                		}
                	}else{
                		return d.remaFollowUTime;
                	}
                }}
                ,{title: '操作', fixed: 'right', align: 'center',width:'140',templet : function(d){
                	var div = $("#detail").html();
                	var html = "";
                	html += '<a class="layui-text-blue layui-btn-xs" id="upDetail" lay-event="upDetail">'+d.upFlowMsg+'</a>';
                	if(div != null && div != undefined) {
                		html += '<a class="layui-text-blue layui-btn-xs" id="detail" lay-event="detail">'+div+'</a>';
                	}
                	return html;
                }}
            ]]
            ,page: true //是否显示分页
        });
       
        form.on('select(selectStore)', function(data){
        	selectStore();
        });
        
        //监听工具条
        table.on('tool(listTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){
                var index = layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: '线索详情'
                    ,content: hostUrl+'/clue/followUpClue/clueDetail.do?clueType=3&clueId='+ data.clue_id
                    ,area: ['500px', '500px']
                    ,btn: ['返回']
                });
                layer.full(index);
            }
            
            if(obj.event === 'upDetail'){
            	dataDetail=data;
                var index = layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: '线索跟进'
                    ,content: hostUrl+'/clue/followUpClue/manyClueFollowUp.do'
                    ,area: ['500px', '500px']
	                ,end: function(){
	                	dataDetail=null;
                    	location.reload();
	                }
                });
                layer.full(index);
            }
        });
        
        $("#inputOptions").click(function () {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '新建线索'
                ,content: hostUrl+'/clue/newClue/storeSaleNewClue.do'
                ,area: ['500px', '500px']
	            ,end: function(){
	            	location.reload();
	            }
            });
            layer.full(index);
        })
        //表刷新方法
        var reloadTable = function () {
            table.reload("listReload", { //此处是上文提到的 初始化标识id
                where: {
                    name: $("#name").val(),
                    phone: $("#phone").val(),
                    storeDistributeTimeStart: $("#start").val(),
                    storeDistributeTimeEnd: $("#end").val(),
                    accType: $("#accType").val(),
                    clueOlevel: $("#clueOlevel").val(),
                    cluePlatformStatus: $("#cluePlatformStatus").val(),
                    clueSourceType: $("#clueSourceType").val()
                }
            });
        };
        $("#search-button").on("click",function () {
            reloadTable();
        })
    });
    layui.use(['laydate'], function(){
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#start'
        });
        laydate.render({
            elem: '#end'
        });
        
    });
});